//
//  TKNetWorkingManager.m
//  XiaoNongDingClient
//
//  Created by admin on 2017/4/25.
//  Copyright © 2017年 ryden. All rights reserved.
//

#import "NWNetWorkingManager.h"
#import "AFHTTPSessionManager+Shared.h"
#import "NSDictionary+RITLNetWorking.h"
#import "RITLKit.h"

@implementation NWNetWorkingManager

#define  ROOT_URL @"http://dtcustomer.bestonepay.com/ngcustomer/"

+ (instancetype)sharedNetWorkingManager {
    
    static NWNetWorkingManager *manager;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        manager = [[NWNetWorkingManager alloc] init];
    });
    
    return manager;
}

+ (NSURLSessionDataTask *)requestPostUrlString:(NSString *)urlString Parameters:(NSDictionary *)parameters success:(NetWorkingSuccessBlock)successBlock failure:(NetWorkingFailureBlock)failureBlock {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedInstance];
    
    //    BOOL isDictionary = [parameters isKindOfClass:NSDictionary.class];
    id parametersInfo = [parameters isKindOfClass:NSDictionary.class] ? [NSDictionary ritl_AppendExtensionAtData:parameters] : parameters;
    NSLog(@"\n\nurl = %@\ninfo = %@\n",urlString,parametersInfo);
    
    // 请求成功的回调
    void(^successHandler)(NSURLSessionDataTask * _Nonnull, id  _Nullable) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        
        if (!successBlock) { return; }
        
        NSError *error;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        
        if (error) { failureBlock(task,error); return; }
        
        successBlock(task,dict);
    };
    
    
    // 请求失败的回调
    void(^failHandler)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        if (!failureBlock) { return; }
        failureBlock(task,error);
    };
    
    if ([[urlString substringToIndex:4] isEqualToString:@"http"]) {  // 完整路径不增加url头
        urlString = urlString;
    }
    else {
        urlString = [ROOT_URL stringByAppendingString: urlString];
    }
    
    return [manager POST:urlString parameters:parametersInfo progress:nil success:successHandler failure:failHandler];
    return nil;
}



+ (NSURLSessionDataTask *)requestWithUrlString:(NSString *)urlString Method:(HTTPMethod)httpMethod Parameters:(NSDictionary *)parameters success:(NetWorkingSuccessBlock)successBlock failure:(NetWorkingFailureBlock)failureBlock {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedInstance];
    
//    BOOL isDictionary = [parameters isKindOfClass:NSDictionary.class];
    id parametersInfo = [parameters isKindOfClass:NSDictionary.class] ? [NSDictionary ritl_AppendExtensionAtData:parameters] : parameters;
    NSLog(@"\n\nurl = %@\ninfo = %@\n",urlString,parametersInfo);
    
    // 请求成功的回调
    void(^successHandler)(NSURLSessionDataTask * _Nonnull, id  _Nullable) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        
        if (!successBlock) { return; }
        
        NSError *error;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        
        if (error) { failureBlock(task,error); return; }
        
        successBlock(task,dict);
    };
    
    
    // 请求失败的回调
    void(^failHandler)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        if (!failureBlock) { return; }
        failureBlock(task,error);
    };
    
    
    switch (httpMethod) {
        case HTTP_GET: {
            return [manager GET:urlString parameters:parametersInfo progress:nil success:successHandler failure:failHandler]; break;
        }
        case HTTP_POST: {
            return [manager POST:urlString parameters:parametersInfo progress:nil success:successHandler failure:failHandler]; break;
        }
        default: break;
    }
    return nil;
}

@end
